package com.zywl.utils.utils;

import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;

import javax.servlet.http.HttpServletResponse;


public final class FileDownloader {
	/**
	 * 本地文件下载
	 * @param response 响应
	 * @param filePath 文件路径
	 * @throws Exception
	 */
	public static void download(HttpServletResponse response,String filePath) throws Exception{
		OutputStream out = null;
		InputStream in = null;
		try{
			String fileName = FileUtil.getFileName(filePath);
			response.reset();
			response.setContentType("bin");
			response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "utf-8"));
			// 构造子节输入流
			in = new FileInputStream(filePath);
	        // 构造子节输出流
	        out = response.getOutputStream();
	        byte[] b = new byte[1024];
	        int len = 0;
	        while ((len = in.read(b)) > 0) {
	        	out.write(b, 0, len);
	        }
	        out.flush();
		}catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("下载文件失败", e);
		}finally{
			try{
				in.close();
			}catch (Exception e) {
			}
			try{
				out.close();
			}catch (Exception e) {
			}
		}
	}
	/**
	 * 下载为CSV文件
	 * @param response
	 * @param fileName 文件名(example.csv)
	 * @param colNameArray 列名称,可为空
	 * @param dataRowArray 数据集合
	 * @throws Exception
	 */
	public static void downloadCSV(HttpServletResponse response,String fileName,String[] colNameArray,String[][] dataRowArray) throws Exception{
		downloadCSV(response, fileName, colNameArray, dataRowArray, "gbk");
	}
	public static void downloadCSV(HttpServletResponse response,String fileName,String[] colNameArray,String[][] dataRowArray,String charset) throws Exception{
		OutputStream out = null;
		try{
			response.reset();
			response.setContentType("bin");
			response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, charset));
			StringBuffer resultBuffer = new StringBuffer();
			int rowCounter = 0;
			if(colNameArray != null && colNameArray.length != 0){
				int colNameCounter = 0;
				for(String colName : colNameArray){
					if(colNameCounter != 0){
						resultBuffer.append(",");
					}
					resultBuffer.append(colName);
					colNameCounter++;
				}
				resultBuffer.append("\n");
			}
			for(String[] strArray : dataRowArray){
				if(rowCounter != 0){
					resultBuffer.append("\n");
				}
				int colCounter = 0;
				for(String str : strArray){
					if(colCounter != 0){
						resultBuffer.append(",");
					}
					resultBuffer.append(str);
					colCounter++;
				}
				rowCounter++;
			}
	        // 构造子节输出流
	        out = response.getOutputStream();
//	        out.write(new   byte []{( byte ) 0xEF ,( byte ) 0xBB ,( byte ) 0xBF });
	        out.write(resultBuffer.toString().getBytes(charset));
	        out.flush();
		}catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("下载文件失败", e);
		}finally{
			try{
				out.close();
			}catch (Exception e) {
			}
		}
	}
}